﻿using Microsoft.AspNetCore.Mvc;
using IBLL;
using System.Threading.Tasks;
using System;
using Dtos;

namespace _2022_2_28.Controllers
{
    public class HomeWorkController : Controller
    {
        private IHomeWorkBll _bll;
        public HomeWorkController(IHomeWorkBll bll)
        {
            _bll = bll;
        }
        public async Task<IActionResult> Index()
        {
            var data = await _bll.GetAll();
            return View(data);
        }

        [HttpGet]
        public async Task<IActionResult> Add() 
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Add(string title,DateTime publishTime,DateTime endTime,DateTime createTime,DateTime updateTime) 
        {
            var data = await _bll.AddHomeWorkAsync(title, publishTime, endTime,createTime,updateTime);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public async Task<IActionResult> Update(Guid id) 
        {
            var date = await _bll.GetHomeWorkByIdAsync(id);
            return View(date);
        }

        public async Task<IActionResult> Update(HomeWorkDto hw) 
        {
            var data = await _bll.UpdateHomeWorkAsync(hw.Id,hw.Title,hw.PublishTime,hw.EndTime,hw.UpdateTime);
            return RedirectToAction("Index");
        }

        public async Task<IActionResult> Delete(Guid id) 
        {
            var data = await _bll.DeleteHomeWorkAsync(id);
            return RedirectToAction("Index");
        }

    }
}
